package edu.rutgers.rucampusinfo.directory;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.rutgers.rucampusinfo.PMF;
import edu.rutgers.rucampusinfo.tables.Employee;
import edu.rutgers.rucampusinfo.tables.Roles;
public class Abc extends HttpServlet {
    /**
	 * 
	 */
	private static final long serialVersionUID = -172420556006592977L;
	@SuppressWarnings("unchecked")
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
                throws IOException {
		RequestDispatcher rd = null;
		resp.setContentType("text/html");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		String netid = req.getParameter("netid");
		PrintWriter out = resp.getWriter();				
		try{
			Object[] empinfo = new Object[16];
			Query query = pm.newQuery("SELECT FROM "+Employee.class.getName()+" where netid == \""+netid+"\"");
			List<Employee> results = (List<Employee>)query.execute();
			if(results.iterator().hasNext()){
				for(Employee empdir : results){
					empinfo[0] = empdir.getFname()+" "+empdir.getLname();
					empinfo[1] = empdir.getNetid();
					empinfo[2] = "/generateImage?image="+empdir.getNetid();
					empinfo[3] = empdir.getEmail();
					empinfo[4] = empdir.getMobile();
					empinfo[5] = empdir.getOfficephone();
					empinfo[6] = empdir.getHomephone();
					empinfo[7] = empdir.getHomeaddr();
					empinfo[8] = empdir.getCity();
					empinfo[9] = empdir.getState();
					empinfo[10] = empdir.getZip();
					empinfo[11] = empdir.getGraduation();
					String[] rolesList = null;
					Query query2 = pm.newQuery("SELECT FROM "+Roles.class.getName()+" where netid == \""+netid+"\" order by roleid");
					List<Roles> results2 = (List<Roles>)query2.execute();
					if(results2.iterator().hasNext())
					{
						rolesList = new String[results2.size()];
						int i = 0;
						for(Roles roles : results2){
							rolesList[i] = roles.getRoleid();
							i++;
						}
					}
					empinfo[12] = rolesList;
					empinfo[13] = empdir.getInactive();
					empinfo[14] = empdir.getMi();
					empinfo[15] = empdir.getCollege();
				}
			}
			req.setAttribute("employeeinfo", empinfo);
			rd = getServletContext().getRequestDispatcher("/abc.jsp");
			rd.forward(req, resp);
		}catch(Exception ex)
		{
			ex.printStackTrace(out);
		}
        finally {
        	pm.close();
        }
    }
}